package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
 * Knife4j-Swagger接口文档配置
 */
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfiguration {

    // 文档标题
    private static final String docTitle = "title";
    // 文档说明
    private static final String docDesc = "description";
    // 条款链接
    private static final String termsUrl = "";
    // 接口版本
    private static final String docVersion = "1.0";
    // 分组名称
    private static final String groupName = "groupName";
    // Controller包路径
    private static final String controllerPath = "com.example.controller";

    @Bean
    public Docket getDocket(Environment environment) {
        // 检测当前环境，只有开发环境下才启用
        Profiles profiles = Profiles.of("dev");
        boolean flag = environment.acceptsProfiles(profiles);

        ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
        apiInfoBuilder.title(docTitle);
        apiInfoBuilder.description(docDesc);
        apiInfoBuilder.termsOfServiceUrl(termsUrl);
        apiInfoBuilder.version(docVersion);

        return new Docket(DocumentationType.SWAGGER_2)
                .enable(flag)
                .apiInfo(apiInfoBuilder.build())
                .groupName(groupName)
                .select()
                .apis(RequestHandlerSelectors.basePackage(controllerPath))
                .paths(PathSelectors.any())
                .build();
    }
}
